#!/usr/bin/python

import config
import subprocess

DECORATION_WIDTH = 50


def interactive_stuff_selection(stuffs):
    stuffs_accepted = {}
    print('[ 0 ] all')
    for index, stuff in enumerate(stuffs):
        number = index + 1
        if index == len(stuffs.keys()) - 1:
            endline = '\n'
        else:
            endline = '\t' if number % 3 else '\n'
        print(f'[ {number} ] {stuff}', end=endline)
    answer_list = input('--> ') \
            .strip() \
            .replace(',', ' ') \
            .split()
    for number in answer_list:
        number = int(number)
        if number == 0:
            stuffs_accepted = stuffs.copy()
            return stuffs_accepted
        elif 0 < number <= len(stuffs.keys()):
            stuff_name = list(stuffs.keys())[number - 1]
            stuffs_accepted[stuff_name] = stuffs[stuff_name]
        else:
            print(f'Warning: Number {number} is too high or too low. Ignoring')
            answer_list.remove(str(number))
    return stuffs_accepted


def install_packages(command, packages_list):
    global DECORATION_WIDTH
    print(f' Dependencies: {", ".join(packages_list)} '.center(DECORATION_WIDTH, '-'))
    return subprocess.call(command.split() + packages_list)


def install_stuff(install_command, stuff_name):
    global DECORATION_WIDTH
    print(f' Stuff script: {stuff_name} '.center(DECORATION_WIDTH, '-'))
    return subprocess.call(install_command.split())


def ask(message, function=lambda string: string.lower() in ['yes', 'y','']):
    return function(input(message))


def only_unresolved_dependencies(dependecies, resolved_dependencies):
    unresolved_dependecies = []
    for dependency in dependecies:
        if not dependency in resolved_dependencies:
            unresolved_dependecies.append(dependency)
    return unresolved_dependecies


os_install_command = None
resolved_depends = []
install_error_depends = []

stuffs_to_install = interactive_stuff_selection(config.stuffs)
for stuff, value in stuffs_to_install.items():
    script, depends = value
    dependecies_to_install = only_unresolved_dependencies(depends, resolved_depends)
    print(f' Stuff: {stuff} '.center(DECORATION_WIDTH, '='))
    if not script and not depends:
        print('Nothing to do')
    is_to_install_depends = ask(f'Do you wanna install [{", ".join(dependecies_to_install)}]? ') if dependecies_to_install else False
    if is_to_install_depends:
        if not os_install_command:
            os_install_command = input('system\'s install command: ')
        if install_packages(os_install_command, dependecies_to_install) == 0:
            resolved_depends.extend(dependecies_to_install)
        else:
            input(f'!!! Install [{", ".join(dependecies_to_install)}] fail. Press ENTER to continue')
            install_error_depends.extend(dependecies_to_install)
    if script:
        install_stuff(script, stuff)
if len(install_error_depends):
    print(f'Failed to install packages {install_error_depends}')

